package com.sl.sdn.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.stream.CollectorUtil;
import cn.hutool.core.stream.StreamUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyuncs.ecs.transform.v20140526.DescribeTagsResponseUnmarshaller;
import com.sl.sdn.dto.OrganDTO;
import com.sl.sdn.dto.TransportLineNodeDTO;
import com.sl.sdn.entity.node.AgencyEntity;
import com.sl.sdn.enums.OrganTypeEnum;
import com.sl.sdn.repository.TransportLineRepository;
import com.sl.sdn.utils.CypherUtil;
import org.neo4j.driver.internal.value.PathValue;
import org.neo4j.driver.types.Path;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class TransportLineRepositoryImpl implements TransportLineRepository {
    @Resource
    private Neo4jClient neo4jClient;

    @Override
    public TransportLineNodeDTO findShortestPath(AgencyEntity start, AgencyEntity end) {
        // 获取结点类型
        String nodeType = AgencyEntity.class.getAnnotation(Node.class).value()[0];
        // 需要执行的cypher语句
        String cypher = StrUtil.format("match path = shortestPath((n:{}) -[*..10]-> (m:{}))\n" +
                "where n.bid = $startId and m.bid = $endId \n" +
                "return path", nodeType, nodeType);
        // 封装数据
        return CypherUtil.postting(neo4jClient, cypher, start.getBid(), end.getBid());
    }


    @Override
    public TransportLineNodeDTO findCostLowestPath(AgencyEntity start, AgencyEntity end) {
        // 获取结点类型
        String nodeType = AgencyEntity.class.getAnnotation(Node.class).value()[0];
        // 需要执行的cypher语句
        String cypher = StrUtil.format("match path = (n:{}) -[*..10]-> (m:{})\n" +
                "where n.bid = $startId and m.bid = $endId \n" +
                "unwind relationships(path) as r \n" +
                "with sum(r.cost) as cost, path\n" +
                "return path order by cost asc, length(path) asc limit 1", nodeType, nodeType);
        // 封装数据
        return CypherUtil.postting(neo4jClient, cypher, start.getBid(), end.getBid());
    }
}
